<template>
    <div class="store-header">
        <WhiteSpace/>
        <div class="store-info">
            <van-image lazy-load width="48" height="48" :radius="4" :src="logo"/>
            <div class="store-layout">
                <wing-blank class="store-layout-name">
                    <span class="store-name">{{store.name}}</span>
                    <span class="store-followers"><number-count v-model="followerCount"/>人关注</span>
                </wing-blank>
                <van-button round plain icon="like"
                            size="small" type="primary"
                            @click.stop="handleUnfollowingStore" v-if="storeFollowing">
                    已关注
                </van-button>
                <van-button round plain icon="like-o"
                            size="small" type="primary"
                            @click.stop="handleFollowingStore" v-else>
                    关注
                </van-button>
            </div>
        </div>
        <WhiteSpace/>
    </div>
</template>

<script>
  import { Button, Col, Image, Row, Toast } from "vant"
  import WhiteSpace from "@/components/WhiteSpace"
  import WingBlank from "@/components/WingBlank"
  import { follow, security, store as Store } from "@mall/api-services"
  import * as _ from "lodash"
  import NumberCount from "@/components/NumberCount"

  export default {
    name: "StoreHeader",
    components: {
      NumberCount,
      WhiteSpace,
      WingBlank,
      [Button.name]: Button,
      [Col.name]: Col,
      [Image.name]: Image,
      [Row.name]: Row,
    },
    props: {
      store: Store.Store,
    },
    watch: {
      store(store) {
        if (!_.isUndefined(store.id)) {
          this.logo = store.logo
          this.refreshFollowingStore()
          this.refreshStoreFollowerCount(store.id)
        }
      },
    },
    data() {
      return {
        logo: "",
        storeFollowing: false,
        followerCount: 0,
      }
    },
    methods: {
      refreshStoreFollowerCount(storeId) {
        follow.FollowServiceV1.getStoreFollowerCount(storeId)
          .then(count => this.followerCount = count)
      },
      refreshFollowingStore() {
        const customerId = security.SecurityUserHolder.getUserId()
        const { store } = this
        follow.FollowServiceV1.checkFollowingStore(customerId, store.id)
          .then(storeFollowing => this.storeFollowing = storeFollowing)
      },
      handleFollowingStore() {
        if (!security.SubjectHolder.isAuthenticated()) {
          this.$router.push("/login")
          return
        }
        const { store } = this
        this.storeFollowing = true
        const customerId = security.SecurityUserHolder.getUserId()
        const toastLoading = Toast.loading()
        follow.FollowServiceV1.followingStore(customerId, store.id)
          .then(() => toastLoading.clear())
          .then(() => {
            Toast({
              message: "关注成功",
              icon: "like",
            })
          })
          .finally(() => this.storeFollowing = false)
      },
      handleUnfollowingStore() {
        const { store } = this
        this.storeFollowing = false
        const customerId = security.SecurityUserHolder.getUserId()
        const toastLoading = Toast.loading()
        follow.FollowServiceV1.unfollowingStore(customerId, store.id)
          .then(() => toastLoading.clear())
          .then(() => {
            Toast({
              message: "已取消关注",
              icon: "like-o",
            })
          })
          .catch(() => this.storeFollowing = true)
      },
    },
  }
</script>

<style lang="scss" scoped>
    @import "../../styles/variables";

    .store-header {
        .store-info {
            display: flex;
        }

        .store-info {
            display: flex;

            .store-layout {
                flex: 1;
                display: flex;
                justify-content: space-between;
                align-items: center;

                .store-layout-name {
                    display: inline-flex;
                    flex-direction: column;
                    justify-content: space-evenly;

                    .store-name {
                        font-size: 16px;
                        font-weight: 700;
                    }

                    .store-followers {
                        font-size: 12px;
                        color: $gray-7;
                    }
                }
            }
        }
    }
</style>
